<?php
class AttrModel extends Model {
    public function __construct() {
        parent::__construct();
        //manage表需要的字段
        $this->fields = array('id','name','way','item','nav');
        $this->tables = array(DB_FREFIX.'attr');
        $this->check = new AttrCheck();
        //转义post和get获得的各种参数
        list(
                $this->R['id'],
                $this->R['name'],
                $this->R['goodsid']
        ) = $this->getRequest()->getParams(
            array(
                isset($_GET['id']) ? $_GET['id'] : null,
                isset($_POST['name']) ? $_POST['name'] : null,
                isset($_GET['goodsid']) ? $_GET['goodsid'] : null
            )
        );
    }

    public function findAll() {
        $allAttr = parent::select($this->tables,array('id','item','name','nav'));
        $allNav = Tool::setObjectArray(parent::select(array(DB_FREFIX.'nav'),array('id','name')),'id','name');
        foreach($allAttr as $value) {
            if(!Validate::isNullString($value->nav)) {
                $temp = explode(',',$value->nav);
                $value->nav = '';
                foreach($temp as $v) {
                    $value->nav .= $allNav[$v].',';
                }
                $value->nav = substr($value->nav,0,-1);
            }
        }
        return $allAttr;
    }

    public function findOne() {
        $condition = array('id='.$this->R['id']);
        $oneAttr = parent::select($this->tables,array('id','name','way','item','nav'),array('where'=>$condition));
        foreach($oneAttr as $value) {
            $value->nav = explode(',',$value->nav);
        }
        return $oneAttr;
    }

    public function add() {
        $condition = array('name='.$this->R['name']);
        if(!$this->check->addCheck($this,$condition)) $this->check->displayError();
        $needData = $this->getRequest()->filter($this->fields);
        if(isset($needData['nav'])) $needData['nav'] = implode(',',$needData['nav']);
        return parent::add($needData);
    }

    public function update() {
        $condition = array('id='.$this->R['id']);
        $oneAttr = parent::select($this->tables,array('name'),array('where'=>$condition));
        $attrName = $oneAttr[0]->name;
        if(!$this->check->updateCheck($this,$condition)) $this->check->displayError();
        $updateData = $this->getRequest()->filter($this->fields);
        if(isset($updateData['nav'])) {
            //在取消或者关联了某个属性后，立刻对goods表中对应的attr属性进行修改
            $updateData['nav'] = implode(',',$updateData['nav']);
            //筛选出更新的时候没有被关联的导航id
            $otherNav = parent::select(array(DB_FREFIX.'nav'),array('id'),array('where'=>array('id not in ('.$updateData['nav'].') AND sid <> 0')));
            $otherId = '';
            if(isset($otherNav)) {
                foreach($otherNav as $value) {
                    $otherId .= $value->id.',';
                }
            }
            $otherId = substr($otherId,0,-1);
            $this->tables = array(DB_FREFIX.'goods');
            //筛选出没有被关联的各种商品
            $otherGoods = parent::select($this->tables,array('id','attr'),array('where'=>array('nav in ('.$otherId.') AND attr LIKE \'%'.$attrName.'%\'')));
            $newAttr = '';
            $data = array();
            if(isset($otherGoods)) {
                foreach($otherGoods as $value) {
                    $start = mb_strpos($value->attr,';'.$attrName,0,'utf-8');
                    if(mb_strpos($value->attr,';',$start+1,'utf-8') !== false) {
                        $end = mb_strpos($value->attr,';',$start+1,'utf-8');
                        $lastIndex = mb_strlen($value->attr,'utf-8') - $end;
                        $strHead = mb_substr($value->attr,0,$start,'utf-8');
                        $strEnd = mb_substr($value->attr,-$lastIndex,$lastIndex,'utf-8');
                        $newAttr = $strHead.$strEnd;
                    } else {
                        $newAttr = mb_substr($value->attr,0,$start,'utf-8');
                    }
                    $data = array('attr'=>"$newAttr");
                    parent::update($data,array('where'=>"id='$value->id'"));
                }
            }
        } else {
            $updateData['nav'] = '';
        }
        $this->tables = array(DB_FREFIX.'attr');
        return parent::update($updateData,$condition);
    }

    public function delete() {
        $condition = array('id='.$this->R['id']);
        if(!$this->check->oneCheck($this,$condition)) $this->check->displayError();
        return parent::delete($condition);
    }

    public function findGoodsAttrWay() {
        $condition = array('id='.$this->R['goodsid']);
        $goods = parent::select(array(DB_FREFIX.'goods'),array('attr'),array('where'=>$condition));
        $attr = explode(';',$goods[0]->attr);
        $attrName = '';
        foreach($attr as $key=>$value) {
            $attrName .= mb_substr($value,0,mb_strpos($value,':',0,'utf-8'),'utf-8').',';
        }
        $attrName = mb_substr($attrName,0,-1);
        $attrName = str_replace(',',"','",$attrName);
        $allWay = parent::select($this->tables,array('way'),array('where'=>array("name in('$attrName')")));
        $theWay = '';
        if($allWay) {
            foreach($allWay as $value) {
                $theWay .= $value->way.'|';
            }
            $theWay = substr($theWay,0,-1);
        }
        return $theWay;
    }

    public function getAttr() {
        $allAttr = parent::select($this->tables,array('name','item'),array('like'=>array('nav'=>$_GET['id'])));
        $allAttr = Tool::setObjectArray($allAttr,'name','item');
        $attr = '';
        if($allAttr) {
            foreach($allAttr as $key=>$value) {
                $attr .= $key.':'.$value.';';
            }
            $attr = substr($attr,0,-1);
            echo $attr;
        } else {
            echo '';
        }
    }

    public function checkAttrName() {
        $this->check->checkAttrName($this,array('name='.$this->R['name']));
    }



}


?>